<?php
namespace Base;

abstract class BaseOrmObject implements \ArrayAccess
{
    const PAGE_TYPE_NORMAL = 1; //正常翻页
    const PAGE_TYPE_PULL = 2; // 上拉加载下拉刷新

    const COMPANY_ID = 1;  // 公司id
    const COMPANY_RANK_ID = 6;  // 公司等级
    const COMPANY_RANK_LEVEL = 6;  // 公司等级
    const COMPANY_RANK_THREE_ID = 5;  // 三星id

    const BUSINESS_TYPE_BONUS = 1; // 奖励金
    const BUSINESS_TYPE_PERFORMANCE_REBATE = 2; // 业绩返点
    const BUSINESS_TYPE_PROFIT = 3; // 利润
    const BUSINESS_TYPE_PURCHASE_DEDUCT = 4; // 下级进货扣款
    const BUSINESS_TYPE_PURCHASE_ADD = 5; // 下级进货补款
    const BUSINESS_TYPE_LONG_RECOMMEND = 6; // 长期推荐奖
    const BUSINESS_TYPE_REGISTER = 7; // 注册充值
    const BUSINESS_TYPE_FIRST_RECOMMEND = 8; // 首次推荐奖
    CONST BUSINESS_TYPE_BACKGROUND_RECHARGE = 9;//后台充值
    CONST BUSINESS_TYPE_BACKGROUND_CHARGE = 10;//后台扣款
    CONST BUSINESS_TYPE_SHOP = 11;//直购订单付款
    CONST BUSINESS_TYPE_UPGRADE = 12;//客户升级
    CONST BUSINESS_TYPE_WITHDRAW = 13;//余额提现
    CONST BUSINESS_TYPE_RECHARGE = 14;//货款充值
    const BUSINESS_TYPE_UPGRADE_RECOMMEND = 15; // 升级推荐奖
    const BUSINESS_TYPE_LONG_RECOMMEND_REDUCE = 16; // 发放长期推荐奖

    const BUSINESS_TYPE_DIRECT = 18; // 直接推荐奖
    const BUSINESS_TYPE_INDIRECT = 19; // 间接推荐奖

    /*---------------------------*/

    const PAYMENT_TYPE_WX = 1; // 支付类型 微信
    const PAYMENT_TYPE_ALIPAY = 2; // 支付类型  支付宝
    const PAYMENT_TYPE_BANK = 3; // 支付类型 银行
    const PAYMENT_TYPE_BALANCE = 4; // 支付类型 余额
    const PAYMENT_TYPE_STORAGE = 5; // 支付类型 货款
    const PAYMENT_TYPE_COMPANY = 6; // 支付类型 公司打款
    const PAYMENT_TYPE_OTHER = 7; // 支付类型 其他
    const PAYMENT_TYPE_OFFLINE =8; // 线下支付

    /*---------------------------*/
    const AMOUNT_TYPE_NO = 0; // 资金变动账户 无
    const AMOUNT_TYPE_STORAGE = 1; // 资金变动账户 货款
    const AMOUNT_TYPE_BALANCE = 2; // 资金变动账户 余额
    const AMOUNT_TYPE_COMPANY = 3; // 资金变动账户 公司账户
    const AMOUNT_TYPE_SECURITY = 4; // 资金变动账户 保证金
    const AMOUNT_TYPE_WEIXIN = 5; // 资金变动账户 微信【在线支付】

    const ID_TYPE_PICK_UP = 1; // 提货单
    const ID_TYPE_PURCHASE = 2; // 进货单
    const ID_TYPE_SHOP = 3; // 直购单

    // 设置百分比
    const DIRECT_PERCENT = 0.05; // 直接百分比
    const INDIRECT_PERCENT = 0.03; // 间接百分比

    /**
     * 业务类型列表
     * @param string $key
     * @return array|mixed
     */
    public static function getBusinessTypeList($key='')
    {
        $list = array(
            self::BUSINESS_TYPE_BONUS => '奖励金',
            self::BUSINESS_TYPE_PERFORMANCE_REBATE => '业绩返点',
            self::BUSINESS_TYPE_PROFIT => '利润',
            self::BUSINESS_TYPE_PURCHASE_DEDUCT => '进货扣款',
            self::BUSINESS_TYPE_PURCHASE_ADD => '下级进货补款',
            self::BUSINESS_TYPE_LONG_RECOMMEND=> '长期推荐奖',
            self::BUSINESS_TYPE_REGISTER=> '注册充值',
            self::BUSINESS_TYPE_FIRST_RECOMMEND=> '首次推荐奖',
            self::BUSINESS_TYPE_BACKGROUND_RECHARGE=> '后台充值',
            self::BUSINESS_TYPE_BACKGROUND_CHARGE=> '后台扣款',
            self::BUSINESS_TYPE_UPGRADE=> '客户升级',
            self::BUSINESS_TYPE_WITHDRAW=> '余额提现',
            self::BUSINESS_TYPE_RECHARGE=> '货款充值',
            self::BUSINESS_TYPE_SHOP=> '直购付款',
            self::BUSINESS_TYPE_UPGRADE_RECOMMEND=> '首次推荐奖（升级）',
            self::BUSINESS_TYPE_LONG_RECOMMEND_REDUCE=> '发放长期推荐奖',
            self::BUSINESS_TYPE_DIRECT=> '开发奖',
            self::BUSINESS_TYPE_INDIRECT=> '平推奖',
        );
        if ($key != '') {
            if (isset($list[$key])) {
                return $list[$key];
            } else {
                return '未知';
            }
        }
        return $list;
    }

    /**
     * 获取奖励金
     */
    public static function getBonusStatusIds()
    {
        return array(
            self::BUSINESS_TYPE_LONG_RECOMMEND,
            self::BUSINESS_TYPE_FIRST_RECOMMEND,
            self::BUSINESS_TYPE_UPGRADE_RECOMMEND
        );
    }
    /**
     * 支付类型列表
     * @param string $key
     * @return array|mixed
     */
    public static function getPaymentTypeList($key='')
    {
        $list =  array(
            self::PAYMENT_TYPE_WX => '微信',
            self::PAYMENT_TYPE_ALIPAY => '支付宝',
            self::PAYMENT_TYPE_BANK => '银行',
            self::PAYMENT_TYPE_BALANCE => '余额',
            self::PAYMENT_TYPE_STORAGE => '货款',
            self::PAYMENT_TYPE_COMPANY => '公司打款',
            self::PAYMENT_TYPE_OTHER =>'其他',
            self::PAYMENT_TYPE_OFFLINE =>'线下支付'
        );
        if ($key != '') {
            if (isset($list[$key])) {
                return $list[$key];
            } else {
                return '未知';
            }
        }
        return $list;
    }

    /**
     * 根据业务类型获取是 +还是-
     * @param $business_id
     * @param $customer_id
     * @param $from_customer_id
     * @param $to_customer_id
     * @return string
     */
    public static function getBusinessTypeAssign($business_id,$customer_id,$from_customer_id,$to_customer_id)
    {
        $assign = '-';
        switch ($business_id) {
            case self::BUSINESS_TYPE_BONUS:
            case self::BUSINESS_TYPE_PERFORMANCE_REBATE:
            case self::BUSINESS_TYPE_PROFIT:
            case self::BUSINESS_TYPE_PURCHASE_ADD:
            case self::BUSINESS_TYPE_LONG_RECOMMEND:
            case self::BUSINESS_TYPE_REGISTER:
            case self::BUSINESS_TYPE_FIRST_RECOMMEND:
            case self::BUSINESS_TYPE_BACKGROUND_RECHARGE:
            case self::BUSINESS_TYPE_UPGRADE:
            case self::BUSINESS_TYPE_RECHARGE:
            case self::BUSINESS_TYPE_UPGRADE_RECOMMEND:
            case self::BUSINESS_TYPE_LONG_RECOMMEND_REDUCE:
                $assign = '+';
                break;
            case self::BUSINESS_TYPE_DIRECT:
            case self::BUSINESS_TYPE_INDIRECT:
                if ($customer_id == $from_customer_id) {
                    $assign = '-';
                } else {
                    $assign = '+';
                }
        }
        return $assign;
    }

    /**
     * 资金变动账户类型列表
     * @param string $key
     * @return array|mixed
     */
    public static function getAmountTypeList($key='')
    {
        $list = array(
            self::AMOUNT_TYPE_NO => '无',
            self::AMOUNT_TYPE_STORAGE => '货款',
            self::AMOUNT_TYPE_BALANCE => '奖励',
            self::AMOUNT_TYPE_COMPANY => '公司',
            self::AMOUNT_TYPE_SECURITY => '保证金',
            self::AMOUNT_TYPE_WEIXIN => '微信'
        );
        if ($key != '') {
            if (isset($list[$key])) {
                return $list[$key];
            } else {
                return '未知';
            }
        }
        return $list;
    }
    //--------------------------------------------------------------------------------------------//
    //	functions to implemet for ArrayAccess
    //--------------------------------------------------------------------------------------------//
    public final function offsetSet($offset, $value)
    {
        $this->{$offset} = $value;
    }

    public final function offsetExists($offset)
    {
        return isset($this->{$offset});
    }

    public final function offsetUnset($offset)
    {
        unset($this->{$offset});
    }

    public final function offsetGet($offset)
    {
        return isset($this->{$offset}) ? $this->{$offset} : null;
    }

    public function __init($data)
    {
        if (empty($data)) {
            return;
        }
        $this->backup_data = array();
        foreach ($data as $k => $v) {
            if (property_exists($this, $k)) {
                $this->{$k} = $v;
                $this->backup_data[$k] = $v;
            }
        }
    }


    protected function setBackupData($fields = array())
    {
        $tmp = array('origin' => array(),
            'change' => array()
        );
        if ($fields) // insert/ update
        {
            foreach ($fields as $k) {
                if (isset($this->backup_data[$k])) {
                    if ($this->backup_data[$k] != $this->{$k}) {
                        $tmp['origin'][$k] = $this->backup_data[$k];
                        $tmp['change'][$k] = $this->{$k};
                    }
                } else {
                    $tmp['change'][$k] = $this->{$k};
                }
            }
            $this->backup_data = $tmp;
        } else // delete
        {
            $tmp['origin'] = $this->backup_data;
            $this->backup_data = $tmp;
        }
    }

    public function beginTransaction()
    {
        $model = new \Base\BaseOrmModel();
        return $model->beginTransaction();
    }

    public function commit()
    {
        $model = new \Base\BaseOrmModel();
        return $model->commit();
    }

    public function rollBack()
    {
        $model = new \Base\BaseOrmModel();
        return $model->rollBack();
    }


    final function objToArray($obj){
        $array = array();
        if (is_object($obj)) {
            foreach ($obj as $key=>$value) {
                $array[$key] = $value;
            }
        }
        return $array;
    }
}